package com.zzw.service.Impl;

import com.zzw.service.RedisCacheService;
import com.zzw.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Redis缓存服务示例实现类
 * 提供常用的缓存操作示例
 */
@Service
public class RedisCacheServiceImpl implements RedisCacheService {

    @Autowired
    private RedisUtil redisUtil;

    // 默认缓存时间（秒）
    private static final long DEFAULT_CACHE_TIME = 1800;

    @Override
    public void setCache(String key, Object value) {
        redisUtil.set(key, value, DEFAULT_CACHE_TIME);
    }

    @Override
    public void setCache(String key, Object value, long timeout) {
        redisUtil.set(key, value, timeout);
    }

    @Override
    public Object getCache(String key) {
        return redisUtil.get(key);
    }

    @Override
    public void deleteCache(String key) {
        redisUtil.del(key);
    }

    @Override
    public boolean hasKey(String key) {
        return redisUtil.hasKey(key);
    }

    @Override
    public void refreshExpire(String key) {
        redisUtil.expire(key, DEFAULT_CACHE_TIME);
    }

    @Override
    public void refreshExpire(String key, long timeout) {
        redisUtil.expire(key, timeout);
    }
}
